m.controller("showcontroller",function($rootScope,$scope) {
	
	 
$scope.init = function(){	
	
	$scope.id = $rootScope.getParam("id");

	$scope.number = 0;
	$.ajax({
            type:"POST",
            url:"/commodity/getCommById",
            data:{"id":$scope.id},
            contentType:"application/x-www-form-urlencoded",
            dataType:"json",
            success:function(data){
            	if(data.value.isSelled){
            		$.ajax({
                        type:"POST",
                        url:"/selled/getSelledByCommId",
                        data:{"id":data.value.id},
                        contentType:"application/x-www-form-urlencoded",
                        dataType:"json",
                        success:function(data){
                        	$scope.$apply(function(){
                        		$scope.selledPrice = data.value.price;
                           	 $scope.selledNumber = data.value.number;
                        	}); 
                        }
                        });
            	}
            	 $scope.$apply(function(){
                     
                     $scope.detail = data.value;
                     
                     });
                 }
            	
            		
            	});

	}
		

$scope.init();

	$scope.findOne = function(array,id){
		return array.filter(function(item){
			return item.commodity_id == id;
		})[0];
	}

	$scope.addCart = function(){ 
		var obj = {};
        obj['commodity_id'] = $scope.detail.id;
        obj['number'] = $scope.number;
        obj['price'] = $scope.detail.price;
        obj['name'] = $scope.detail.name;
        var commodityList1 = new Array;
        var commodityList = $rootScope.getCookie("commodity");
        if( commodityList == null || commodityList =="" ){
        	commodityList1.push(obj); 
        	$rootScope.setCookie("commodity",commodityList1);
        }else if($scope.findOne(commodityList,$scope.detail.id)){
        	var item = $scope.findOne(commodityList,$scope.detail.id);
        	
			item.number = parseInt(item.number) + parseInt($scope.number);
			$rootScope.setCookie("commodity",commodityList);
        }else{
        	commodityList.push(obj);
        	$rootScope.setCookie("commodity",commodityList);
        }
        alert("添加购物车成功！");
        $scope.init();
	}
	
	$scope.addCartModal = function(){ 
		
		if($scope.number>0){
			$("#modalid-add-show").modal("toggle");
            
		}else{
			alert("您没有购买任何商品！");
		}
		
	}
	
	
//	$scope.addNum = function(){
//		console.log("num"+$scope.number);
//		$scope.$applyAsync(function(){$scope.number += 1;});
//	}
//	$scope.lessNum = function(){
//		console.log("num"+$scope.number);
//		$scope.$applyAsync(function(){$scope.number -= 1;});
//		}
	
});


